import { Button, Space, Tooltip } from 'antd';
import { SaveOutlined, CloseOutlined } from '@ant-design/icons';
import React from 'react';

interface ActionButtonsProps {
    item: API.DockerComposeVO;
    expandedCards: Record<string, boolean>;
    editingCards: Record<string, boolean>;
    onStart: (id: string) => void;
    onStop: (id: string) => void;
    onRestart: (id: string) => void;
    onRebuild: (id: string) => void;
    onEditFile: (id: string) => void;
    onSaveEdit: (id: string) => void;
    onCancelEdit: (id: string) => void;
    onToggle: (id: string) => void;
}

const ActionButtons: React.FC<ActionButtonsProps> = ({
                                                         item,
                                                         expandedCards,
                                                         editingCards,
                                                         onStart,
                                                         onStop,
                                                         onRestart,
                                                         onRebuild,
                                                         onEditFile,
                                                         onSaveEdit,
                                                         onCancelEdit,
                                                         onToggle,
                                                     }) => {
    const isEditing = editingCards[item.id];

    // 未安装状态只显示展开/收起按钮
    if (item.status === 'not-found') {
        return (
            <Button
                size="small"
                type="link"
                onClick={() => onToggle(item.id)}
            >
                {expandedCards[item.id] ? '收起' : '展开'}
            </Button>
        );
    }


    if (isEditing) {
        return (
            <Space>
                <Button
                    size="small"
                    type="primary"
                    icon={<SaveOutlined />}
                    onClick={() => onSaveEdit(item.id)}
                >
                    保存
                </Button>
                <Button
                    size="small"
                    icon={<CloseOutlined />}
                    onClick={() => onCancelEdit(item.id)}
                >
                    取消
                </Button>
            </Space>
        );
    }

    return (
        <Space>
            {item.status === 'running' ? (
                <>
                    <Tooltip title="重建只有在编辑文件之后才有效，若文件一致则是启动，不一致才是重建">
                        <Button size="small" onClick={() => onRebuild(item.id)}>
                            重建
                        </Button>
                    </Tooltip>
                    <Button size="small" onClick={() => onRestart(item.id)}>
                        重启
                    </Button>
                    <Button size="small" danger onClick={() => onStop(item.id)}>
                        停止
                    </Button>
                </>
            ) : (
                <Button
                    size="small"
                    type="primary"
                    onClick={() => onStart(item.id)}
                >
                    启动
                </Button>
            )}
            <Button size="small" onClick={() => onEditFile(item.id)}>
                编辑配置
            </Button>
            <Button
                size="small"
                type="link"
                onClick={() => onToggle(item.id)}
            >
                {expandedCards[item.id] ? '收起' : '展开'}
            </Button>
        </Space>
    );
};

export default ActionButtons;
